package com.mycjj.android.activity;

import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.mycjj.android.R;
import com.mycjj.android.adapter.TAdapter;
import com.mycjj.android.bean.BaseBean;
import com.mycjj.android.bean.GetsCashBaseBean;
import com.mycjj.android.bean.GetsCashBaseBean.GetsCashBean.CashBean;
import com.mycjj.android.http.ProjectHttpRequestImpl;
import com.mycjj.android.http.ProjectHttpRequestInterface;
import com.mycjj.android.pullrefresh.PullToRefreshBase;
import com.mycjj.android.pullrefresh.PullToRefreshBase.OnRefreshListener2;
import com.mycjj.android.pullrefresh.PullToRefreshGridView;
import com.mycjj.android.util.Preferences;

//元宝兑换
public class Score_GoldExchangeActivity extends BaseActivity{
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_score_gold_exchange);
		initHeader();
		init();
		initData();
	}
	
	private ImageView mHeaderLeftImageview;
	private Button mHeaderRightButton;
	private TextView mHeaderTitleTextView;

	private void initHeader() {
		mHeaderLeftImageview = (ImageView) findViewById(R.id.header_left_button);
		mHeaderLeftImageview.setImageResource(R.drawable.back);
		mHeaderRightButton = (Button) findViewById(R.id.header_right_button);
		mHeaderRightButton.setVisibility(View.GONE);
		mHeaderTitleTextView = (TextView) findViewById(R.id.header_title_textview);
		mHeaderTitleTextView.setText("元宝兑换");
		mHeaderLeftImageview.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Score_GoldExchangeActivity.this.finish();
			}
		});
	}
	
	private PullToRefreshGridView gridview;
	
	private void init(){
		gridview = (PullToRefreshGridView) findViewById(R.id.gridview);
		adapter = new ExchangeAdapter(this);
		gridview.setAdapter(adapter);
		gridview.setOnRefreshListener(new OnRefreshListener2<GridView>() {

			@Override
			public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) {
				gridview.onRefreshComplete();
			}

			@Override
			public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {
				gridview.onRefreshComplete();
			}
		});
	}
	
	private void initData(){
		request = new ProjectHttpRequestImpl(this, true);
		RequestParams params = new RequestParams();
		params.put("limit", "100");
		params.put("offset", "0");
		params.put("client_id", ProjectHttpRequestInterface.client_id_key);
		String token = Preferences.getSharedPreferences(this, "user_token", "");
		params.put("token", token);
		request.requestGoodsGetsCashWithDoneHandler("", params, new AsyncHttpResponseHandler(){
			@Override
			public void onSuccess(String content) {
				GetsCashBaseBean baseBean = dataManager.getCashBaseBean();
				if(baseBean != null){
					if(baseBean.getCode() == 200){
						if(baseBean.getData() != null){
							adapter.setList(baseBean.getData().getList());
						}else{
							Log.e("mark", "GetsCashBaseBean.getData() is null!");
						}
					}else{
						Toast.makeText(Score_GoldExchangeActivity.this, baseBean.getMsg(), Toast.LENGTH_SHORT).show();
					}
				}else{
					Log.e("mark", "GetsCashBaseBean is null!");
				}
			}
		});
	}
	
	private ExchangeAdapter adapter;
	public class ExchangeAdapter extends TAdapter<CashBean>{

		public ExchangeAdapter(Context mContext) {
			super(mContext);
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				convertView = LayoutInflater.from(mContext).inflate(R.layout.item_score_gold_exchage_view, null);
				holder = new ViewHolder();
				holder.pic = (ImageView)convertView.findViewById(R.id.iv_pic);
				holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
				holder.tv_price = (TextView) convertView.findViewById(R.id.tv_price);
				holder.btn_exchage = (Button) convertView.findViewById(R.id.btn_exchage);
				convertView.setTag(holder);
			}else{
				holder = (ViewHolder) convertView.getTag();
			}
			final CashBean bean = mList.get(position);
			imageLoader.displayImage(bean.getImage(), holder.pic);
			holder.tv_name.setText(bean.getName());
			holder.tv_price.setText("价值"+bean.getPrice()+"元");
//			if("1".equals(bean.getIslimit())){
//				
//			}else{
//				holder.btn_exchage.setBackgroundColor(Color.parseColor("#e83710"));
//			}
			int currentPoint = 0;
			int points = dataManager.getPoints();
			try{
				currentPoint = Integer.parseInt(bean.getPoints());
			}catch(Exception e){
				currentPoint = 0;
			}
			if(points < currentPoint){
				holder.btn_exchage.setBackgroundColor(Color.parseColor("#999999"));
				holder.btn_exchage.setEnabled(false);
			}else{
				holder.btn_exchage.setBackgroundColor(Color.parseColor("#e83710"));
				holder.btn_exchage.setEnabled(true);
			}
			holder.btn_exchage.setText(bean.getPoints()+"兑换");
			holder.btn_exchage.setTag(bean);
			
			holder.btn_exchage.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					exchange(v.getTag());//兑奖
				}
			});
			return convertView;
		}
		int needPoints = 0;
		private void exchange(Object tag){
			Log.d("mark", "exchage");
			CashBean bean = (CashBean)tag;
			try{
				needPoints = Integer.parseInt(bean.getPoints());
			}catch(Exception e){
				needPoints = 0;
			}
			request = new ProjectHttpRequestImpl(mContext, true);
			final String goods_id = bean.getId();
			RequestParams params = new RequestParams();
			params.put("goods_id", goods_id);
			params.put("client_id", ProjectHttpRequestInterface.client_id_key);
			String token = Preferences.getSharedPreferences(mContext, "user_token", "");
			params.put("token", token);
			request.requestSyceeDoCashWithDoneHandler("", params, new AsyncHttpResponseHandler(){
				
				@Override
				public void onSuccess(String content) {
					BaseBean<Map> baseBean = (BaseBean<Map>)dataManager.getBaseBean();
					if(baseBean != null){
						int code = baseBean.getCode();
						if(code == 200){
							int point = dataManager.getPoints() - needPoints;
							dataManager.setPoints(point);
							Toast.makeText(mContext, "兑换成功!", Toast.LENGTH_SHORT).show();
						}else if(code == 3322 || code == 3323 || code == 3324){//3322  3323 3324
							Toast.makeText(mContext, "请填写联系人,手机号和邮寄地址！", Toast.LENGTH_SHORT).show();
							Intent intent = new Intent(mContext,AddressInfoActivity.class);
							intent.putExtra("goods_id", goods_id);
							intent.putExtra("needPoints", needPoints);
							mContext.startActivity(intent);
						}else{
							Toast.makeText(mContext, baseBean.getMsg(), Toast.LENGTH_SHORT).show();
						}
					}else{
						Log.e("mark", "baseBean is null!");
					}
				}
			});
		}
		
		
		class ViewHolder{
			ImageView pic;
			TextView tv_name;
			TextView tv_price;
			Button btn_exchage;
			
		}
		
		

	}
}
